<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfBinding">
    
    <ControlTemplate x:Key="validationTemplate">
        <DockPanel>
            <TextBlock Foreground="Red" FontSize="20" DockPanel.Dock="Right" Margin="10,0,0,0">Error!!!</TextBlock>
            <AdornedElementPlaceholder/>
        </DockPanel>
    </ControlTemplate>

    <Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip"
              Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                              Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>

    <Style TargetType="{x:Type local:InputNumberBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:InputNumberBox}">
                    <Border Background="{TemplateBinding Background}"  CornerRadius="4"
                            BorderBrush="{TemplateBinding BorderBrush}" Name="border"
                            BorderThickness="{TemplateBinding BorderThickness}">

                        <TextBox Name="tb" BorderBrush="Black" BorderThickness="0"   Background="Transparent" Foreground="Red"
                               Width="{Binding Width,RelativeSource={RelativeSource  Mode=TemplatedParent}}"
                               Height="{Binding Height,RelativeSource={RelativeSource Mode=TemplatedParent}}"
                        Validation.ErrorTemplate="{StaticResource validationTemplate}"
                        Style="{StaticResource textBoxInError}">
                            <TextBox.Text>
                                <Binding UpdateSourceTrigger="PropertyChanged" Path="Value" 
                                         RelativeSource="{RelativeSource FindAncestor,AncestorType={x:Type local:InputNumberBox}}" 
                                         ValidatesOnDataErrors="True" ValidatesOnExceptions="True" >
                                    <Binding.ValidationRules>
                                        <local:NumberValidationRule  Min="1" Max="100" />
                                    </Binding.ValidationRules>
                                </Binding>
                            </TextBox.Text>
                        </TextBox>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="BorderBrush" TargetName="border"  
                                    Value="{Binding MouseOverBrush,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type local:InputNumberBox}}}"/>
                        </Trigger>
                        <Trigger Property="IsFocused" Value="True">
                            <Setter Property="BorderBrush" TargetName="border"  Value="green"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="BorderBrush" TargetName="border"  Value="Silver"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
